package com.action.sale;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.model.Customer;
import com.model.ItemTemp;

/**
 * Servlet implementation class ServletItemTempServlet
 */
public class SelectItemTempServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		HttpSession session = request.getSession();
		List<ItemTemp> itemTempList = (List<ItemTemp>) session.getAttribute("ItemTempList");
		*/
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		HttpSession session = request.getSession();
		if (session == null || session.getAttribute("ItemTempList") == null) {
			response.getWriter().print("{\"success\":false,\"msg\":\"没有购买的BOOK，请购买！\"}");
			return;
		}
		List<ItemTemp> itemTempList = (List<ItemTemp>) session.getAttribute("ItemTempList");
		int total = itemTempList.size();
//		json格式化
		Map<String, Object> map = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", itemTempList);
		String itemTempJson = JSON.toJSONString(map);
		
//		String itemTempJson = resultToJson(itemTempList, total);
		response.getWriter().print(itemTempJson);
		response.getWriter().flush();
	}

	// json 格式化
	/*private String resultToJson(List<ItemTemp> list, int total) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return JSON.toJSONString(map);
	}*/

}
